package com.daguchuangyi.erp_android.productlist.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.constraint.ConstraintLayout;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.TextView;

import com.daguchuangyi.erp_android.R;
import com.daguchuangyi.erp_android.manager.ParameterManager;
import com.daguchuangyi.erp_android.model.product.Parameter;

/**
 * Created by lijianjian on 2018/3/9.
 */

public class SectionView extends ConstraintLayout {

    private String mTitle;

    private TextView mTitleTv;
    private ImageView mMarkIv;

    private Parameter mParameter;

    private boolean mIsChecked;

    public void bindParameter(Parameter parameter) {
        this.mParameter = parameter;
        mTitle = parameter.getName();
        mTitleTv.setText(mTitle);
        mIsChecked = ParameterManager.getInstance().isChecked(parameter)
                || mParameter.getId() == ParameterManager.ID_PRODUCT_IMAGE_DEFAULT;
        mMarkIv.setVisibility(mIsChecked ? VISIBLE : GONE);
    }

    public Parameter getParameter() {
        return mParameter;
    }

    public boolean isChecked() {
        return mIsChecked;
    }

    public SectionView(Context context) {
        this(context, null);
    }

    public SectionView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public SectionView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SectionView);
            mTitle = ta.getString(R.styleable.SectionView_section_title);
            ta.recycle();
        }

        init(context);

        if (isInEditMode()) {
            mTitleTv.setText("参数类型");
        }
    }

    private void init(Context context) {
        inflate(context, R.layout.view_section_layout, this);
        mTitleTv = findViewById(R.id.title_tv);
        mTitleTv.setText(mTitle);

        mMarkIv = findViewById(R.id.check_mark_iv);
        setOnClickListener(v -> {
            if (mParameter.getId() == ParameterManager.ID_PRODUCT_IMAGE_DEFAULT) {
                return;
            }
            if (isMarked()) {
                mMarkIv.setVisibility(GONE);
                mIsChecked = false;
            } else {
                mMarkIv.setVisibility(VISIBLE);
                mIsChecked = true;
            }
        });
    }

    public boolean isMarked() {
        return mMarkIv.isShown();
    }

    public boolean isLocalParam() {
        return mParameter.getId() == ParameterManager.ID_PRODUCT_IMAGE_DEFAULT
                || mParameter.getId() == ParameterManager.ID_CAD_IMAGE_OPTIONS
                || mParameter.getId() == ParameterManager.ID_SAMPLE_OPTIONS
                || mParameter.getId() == ParameterManager.ID_STOCK_OPTIONS;
    }
}
